package eightpuzzle;

import agent.Action;
import agent.Problem;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class EightPuzzleProblem extends Problem<EightPuzzleState> {

    private EightPuzzleState goalState;

    public EightPuzzleProblem(EightPuzzleState initialState) {
        super(initialState, new ArrayList<Action>());
        super.actions.add(new ActionUp());
        super.actions.add(new ActionDown());
        super.actions.add(new ActionLeft());
        super.actions.add(new ActionRight());
           
        this.goalState = new EightPuzzleState(EightPuzzleState.goalMatrix);
    }
    
    
    
    
    @Override
    public boolean isGoal(EightPuzzleState state) {
        return goalState.equals(state);
    }

    @Override
    public List<EightPuzzleState> executeActions(EightPuzzleState state) {
        
        List<EightPuzzleState> successors = new LinkedList<EightPuzzleState>();
        
        /*para cada uma das acoes disponiveis (cima, baixo, esquerda, direita)
        *se a acao for valida, acrescentar o estado á lista de sucessores
        */
        
        for (Action action : actions) {
            if(action.isValid(state)){
                EightPuzzleState successor = (EightPuzzleState) state.clone();
                action.execute(successor);
                successors.add(successor);                
            }
        }
        
        return successors;
    }

    @Override
    public double computePathCost(List<Action> path) {
        return path.size();
    }

    public EightPuzzleState getGoalState() {
        return goalState;
    }
    
    
    
    
    
}
